package com.ponhu.ea2.goodsCenter.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ponhu.ea2.dto.PhInventoryAppraisalDto;
import com.ponhu.ea2.entity.PhInventoryAppraisal;
import com.ponhu.ea2.goodsCenter.provider.mapper.PhInventoryAppraisalMapper;
import com.ponhu.ea2.goodsCenter.provider.service.PhInventoryAppraisalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ponhu.ea2.goodsCenter.provider.utils.ProBeanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 货品中检结果表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-10-19
 */
@Service
public class PhInventoryAppraisalServiceImpl
        extends ServiceImpl<PhInventoryAppraisalMapper, PhInventoryAppraisal>
        implements PhInventoryAppraisalService {

    /**
     * 根据货品编码获取中检溯源码信息
     *
     * @param inventoryCode
     * @return
     */
    @Override
    public PhInventoryAppraisal getSourceCodeByCode(String inventoryCode) {

        LambdaQueryWrapper<PhInventoryAppraisal> queryWrapper = Wrappers
                .lambdaQuery(PhInventoryAppraisal.class)
                .eq(PhInventoryAppraisal::getInventoryCode, inventoryCode)
                .last("LIMIT 1");

        return getOne(queryWrapper);
    }

    /**
     * 保持货品中检结果
     *
     * @param phInventoryAppraisalDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveInventoryAppraisalByDto(PhInventoryAppraisalDto phInventoryAppraisalDto) {

        PhInventoryAppraisal phInventoryAppraisal = getSourceCodeByCode(phInventoryAppraisalDto.getInventoryCode());
        if (ObjectUtils.isEmpty(phInventoryAppraisal)) phInventoryAppraisal = new PhInventoryAppraisal();

        BeanUtils.copyProperties(phInventoryAppraisalDto, phInventoryAppraisal);

        if (!saveOrUpdate(phInventoryAppraisal)) {
            throw new RuntimeException("保存货品中检信息失败，全局回滚");
        }
    }

}
